package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseOrderStateBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.lease.LeaseOrderListEntity;
import com.sgb.kjwl.view.event.LeaseOrderEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.LeaseOrderStateAdapter;
import com.sgb.kjwl.viewmodel.manage.AgentTotalOrderModel;
import com.sgb.kjwl.viewmodel.self.publish.LeaseOrderStateModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

/**
 * 个人中心--企业代办--租赁-订单列表
 */
public class AgentLeaseOrderStateFragment extends AppLazyFragment {
    private FragmentLeaseOrderStateBinding binding;
    @Inject
    AgentTotalOrderModel viewModel;
    private String status = ""; //订单状态status 1待接单,2 待确认,3待付款，4支付中，5待运输，6待验收,7已完成,8已取消 ,9已拒绝,全部为空
    private String type;//1=求租,2=出租
    private LeaseOrderStateAdapter adapter;
    private boolean isRefresh = false;//是否在懒加载之前刷新过
    private boolean isLoadMore = false; //是否加载更多

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule())
                .build()
                .inject(this);
    }

    public static AgentLeaseOrderStateFragment newInstance(String status, String type) {
        AgentLeaseOrderStateFragment fragment = new AgentLeaseOrderStateFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_order_state, container, false);
        iniArguments();
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    protected void initView() {
        if (adapter == null) {
            adapter = new LeaseOrderStateAdapter(getActivity(), type);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp1), getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(adapter);
        binding.rlvView.setLoadingMoreEnabled(true);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                viewModel.getLeaseOrderList(getContext(), isLoadMore);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                viewModel.getLeaseOrderList(getContext(), isLoadMore);
            }
        });
        adapter.setItemCallBack(new LeaseOrderStateAdapter.ItemCallBack() {


            @Override
            public void detail(String id,String module) {
                viewModel.getH5Url(getContext(), id, type, module);
            }

            @Override
            public void onDelete(String id) {

            }
        });
        viewModel.getLeaseLoadFailed().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean != null){
                    onLoadComplete(aBoolean);
                }
            }
        });
        viewModel.getLeaseOrderList().observe(getViewLifecycleOwner(), new Observer<List<LeaseOrderListEntity>>() {
            @Override
            public void onChanged(List<LeaseOrderListEntity> leaseOrderListEntities) {
                if (leaseOrderListEntities != null){
                    showList(leaseOrderListEntities, isLoadMore);
                }
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        if (!isRefresh){
            initData();
        }
    }

    private void iniArguments() {
        if (getArguments() != null) {
            status =  getArguments().getString("status");
            type = getArguments().getString("type");
        }
    }

    public void initData() {
        isRefresh = true;
        isLoadMore = false;
        viewModel.map.get().put("status", status);
        viewModel.map.get().put("type", type);
        viewModel.getLeaseOrderList(getContext(), false);

    }

    public void showList(List<LeaseOrderListEntity> datas, boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
            if (datas.size() != 0) {
                adapter.addDatas(datas);
            }
        } else {
            if (datas.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.rlvView.refreshComplete();
            binding.rlvView.setLoadingMoreEnabled(true);
            adapter.setDatas(datas);
        }
    }

    public void onLoadComplete(boolean isLoadMore) {
        if (isLoadMore) {
            binding.rlvView.loadMoreComplete();
        } else {
            binding.rlvView.refreshComplete();
        }
        if (!isLoadMore){
            binding.emptyView.setVisibility(View.VISIBLE);
        } else {
            binding.emptyView.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseOrderEvent event) {//刷新
        isLoadMore = false;
        viewModel.getLeaseOrderList(getContext(), isLoadMore);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        isRefresh = false;
    }
}
